﻿using LCL.Config;
using LCL.LData;
using System;
using System.Data;

namespace LCL.DbSqlMonitor
{
    //https://blog.csdn.net/zxian610/article/details/78679985
    public class MySQLImpl : BaseImpl, ISQLMonitor
    {
        public MySQLImpl(DbConfigInfo dbConfig)
            : base(dbConfig)
        {

        }
        public bool DbLinkTest()
        {
            bool flg = true;
            try
            {
                /*
                -- 设置保存到数据库表
                set global log_output='TABLE';
                -- 打开通用日志（任何sql都保存）
                set global general_log=on;
                */
                _dba.ExecuteNonQuery("set global log_output='TABLE';set global general_log=on;");
                flg = true;
            }
            catch (Exception e)
            {
                flg = false;
                Logger.Error("DbLinkTest:", e);
            }
            return flg;
        }

        public DataTable GetSqlALL()
        {
            var dt = _dba.ExecuteDataTable(string.Format(@"select * from mysql.general_log order by event_time desc  LIMIT ", this.RowNum));
            return dt;
        }

        public bool Clear()
        {
            /*
            -- 用完请关闭
            set global general_log=off;
            -- 如果不要，顺道清除掉日志记录
            truncate table mysql.general_log;
            */
            _dba.ExecuteNonQuery("set global general_log=off;truncate table mysql.general_log;");
            return true;
        }
    }
}
